﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace ST.Common.Util.Mvc
{
	/// <summary>
	/// 通用的JSON结果
	/// </summary>
	public class JsonResultBase : JsonResult
	{
		public JsonResultBase() : base(null)
		{
		}

		public JsonResultBase(bool success) : base(null)
		{
			//ContentType = "text/json;charset=UTF-8";
			ResultData ret = new ResultData(success, success ? "请求成功" : "请求失败");
			Value = ret;
		}

		public JsonResultBase(bool success, HttpStatusCode statusCode) : base(null)
		{
			//ContentType = "text/json;charset=UTF-8";
			ResultData ret = new ResultData(success, success ? "请求成功" : "请求失败", statusCode);
			Value = ret;
		}

		public JsonResultBase(bool success, string msg) : base(null)
		{
			//ContentType = "text/json;charset=UTF-8";
			ResultData ret = new ResultData(success, msg);
			Value = ret;
		}

		public JsonResultBase(bool success, string msg, int statusCode) : base(null)
		{
			//ContentType = "text/json;charset=UTF-8";
			ResultData ret = new ResultData(success, msg, statusCode);
			Value = ret;
		}

		public JsonResultBase(object data, HttpStatusCode statusCode) : base(null)
		{
			//ContentType = "text/json;charset=UTF-8";
			if (data is ResultData)
			{
				Value = data;
			}
			else
			{
				ResultData ret = new ResultData(data);
				Value = ret;
			}
		}

		public JsonResultBase(object data, JsonSerializerSettings serializerSettings) : base(null, serializerSettings)
		{
			//ContentType = "text/json;charset=UTF-8";
			if (data is ResultData)
			{
				Value = data;
			}
			else
			{
				ResultData ret = new ResultData(data);
				Value = ret;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="data">如果类型不为ResultData，则将数据封装为ResultData的data</param>
		/// <param name="dateFormat"></param>
		public JsonResultBase(object data, string dateFormat = "yyyy-MM-dd HH:mm:ss") : base(null)
		{
			//ContentType = "text/json;charset=UTF-8";
			if (data is ResultData)
			{
				Value = data;
			}
			else
			{
				ResultData ret = new ResultData(data);
				Value = ret;
			}
			if (!string.IsNullOrWhiteSpace(dateFormat))
			{
				JsonSerializerSettings settings = new JsonSerializerSettings();
				settings.NullValueHandling = NullValueHandling.Ignore;
				settings.DateFormatString = dateFormat;
				SerializerSettings = settings;
			}
		}
	}
}
